<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:fn="http://java.sun.com/jsp/jstl/functions"
                xmlns:ds="http://deltaset.org/deltaset-faces"
                xmlns:meta="http://deltaset.org/deltaset-meta"
                xmlns:mi="http://java.sun.com/jsf/composite/deltaset.meta/internal"
                xmlns:composite="http://java.sun.com/jsf/composite">
    <composite:interface componentType="org.deltaset.metaCrud">
        <composite:attribute name="entityClass" type="java.lang.String" required="true"/>
        <composite:attribute name="repositoryProvider" type="org.deltaset.meta.extension.RepositoryProvider" required="true"/>
		<composite:attribute name="metaType" type="java.lang.String" default="internal"/>
		<composite:attribute name="detailTemplate" type="java.lang.String" default="not_used_internal"/>
		<composite:attribute name="executeTemplate" type="java.lang.String" default="not_used_internal"/>
		<composite:attribute name="formTemplate" type="java.lang.String" default="not_used_internal"/>
		<composite:attribute name="listTemplate" type="java.lang.String" default="not_used_internal"/>
		<composite:attribute name="defaultView" type="java.lang.String" default="listTemplate"/>
        <composite:attribute name="catalogName" type="java.lang.String"/>
    </composite:interface>

    <composite:implementation>
        <c:set var="editPanelDomId" value="#{cc.clientId}:editCrudPanel"/>
        <c:set var="editPanelId" value=":#{editPanelDomId}"/>
        <c:set var="editPanelFormId" value=":#{cc.clientId}:editCrudPanel:edit-form"/>
        <c:set var="editPanelFormDomId" value="#{cc.clientId}:editCrudPanel:edit-form"/>
        <c:set var="pagerId" value=":#{cc.clientId}:pager"/>
        <c:set var="pagerFormId" value="#{cc.clientId}:pager"/>
        <c:set var="editPanelOpen" value="function(data){if('success'==data.status){deltaset.dialog('#{editPanelDomId}').open();}}"/>               
        <c:set var="editPanelClose" value="function(data){if('success'==data.status){deltaset.dialog('#{editPanelDomId}').keepOrClose();}}"/>      
        <c:set var="executePanelDomId" value="#{cc.clientId}:executePanel"/>
        <c:set var="executePanelFormId" value="#{cc.clientId}:executePanel:executor"/>
        <c:set var="executorId" value=":#{cc.clientId}:executePanel:executor"/>
        <c:set var="executePanelOpen" value="function(data){if('success'==data.status){deltaset.dialog('#{executePanelDomId}').open();}}"/>  
        <c:set var="executePanelClose" value="function(data){if('success'==data.status){deltaset.dialog('#{executePanelDomId}').keepOrClose();}}"/>

        <c:set var="switchExecuteForEdit" value="function(data){if('success'==data.status){if(deltaset.dialog('#{executePanelDomId}').keepOrClose()){ deltaset.dialog('#{editPanelDomId}').open(); }}}"/>
        <c:set var="switchEditForExecute" value="function(data){if('success'==data.status){if(deltaset.dialog('#{editPanelDomId}').keepOrClose()){ deltaset.dialog('#{executePanelDomId}').open(); }}}"/>     

        <h:form  id="pager">

            <ds:titlebar title="#{cc.labelEntity}" style="margin-bottom: 5px;">
                <div style="display:block">
                    <c:forEach items="#{cc.factoryActions}" var="action">
                        <mi:commandButton metaAction="#{action}"
                                          model="#{cc.model}"
                                          meta="#{cc.meta}"
                                          noVoidOnEvent="#{editPanelOpen}"
                                          noVoidRender="#{editPanelFormId}"
                                          parameterOnEvent="#{executePanelOpen}"
                                          parameterRender="#{executorId}"
                                          />
                    </c:forEach>

                    <c:forEach items="#{cc.otherActions}" var="action">
                        <mi:commandButton metaAction="#{action}"
                                          model="#{cc.model}"
                                          meta="#{cc.meta}"
                                          noVoidOnEvent="#{executePanelOpen}"
                                          noVoidRender="#{executorId}"
                                          voidOnEvent="#{executePanelOpen}"
                                          voidRender="#{executorId}"
                                          parameterOnEvent="#{executePanelOpen}"
                                          parameterRender="#{executorId}"
                                          style="font-size: 0.8em;"
                                          />
                    </c:forEach>

                </div>
            </ds:titlebar>


            <c:forEach items="#{cc.finderActions}" var="finderAction">             

                <h:commandLink  value="#{cc.labelAction(finderAction)}"                                      
                                action="#{cc.loadFinder(finderAction)}">                    
                    <f:ajax execute="@this" render="@form" />
                </h:commandLink>  <br/>
            </c:forEach>

            <h:messages styleClass="ui-state-highlight ui-corner-all"/>

            <h:panelGroup id="finder">
                <ui:include src="internal/finder.xhtml">
                    <ui:param name="meta" value="#{cc.meta}"/>
                    <ui:param name="formId" value="#{pagerFormId}"/>
                </ui:include>               
            </h:panelGroup>

            <ui:include src="internal/pagedTable.xhtml">
                <ui:param name="meta" value="#{cc.meta}"/>
            </ui:include>

        </h:form>

        <ds:dialog id="editCrudPanel" title="Add/Edit: #{cc.labelEntity}">
            <h:form id="edit-form" styleClass="demo" onsubmit="return false;">
                <ds:dialog-keep-open-if dialogId="#{editPanelDomId}" test="#{true == cc.hasValidationErrors}"/>
                <ui:include src="internal/form.xhtml">
                    <ui:param name="meta" value="#{cc.meta}"/>
                    <ui:param name="formId" value="#{editPanelFormDomId}"/>
                </ui:include>
            </h:form>
        </ds:dialog>

        <ds:dialog id="executePanel" title="Execute method">
            <h:form id="executor">
                <ds:dialog-keep-open-if dialogId="#{executePanelDomId}" test="#{true == cc.hasValidationErrors}"/>
                <ui:include src="internal/executor.xhtml">
                    <ui:param name="meta" value="#{cc.meta}"/>
                    <ui:param name="formId" value="#{executePanelFormId}"/>
                </ui:include>
            </h:form>
        </ds:dialog>
    </composite:implementation>

</ui:composition>
